#! /usr/bin/env python
print 'Loading Modules'
import spectrometer
from RefractiveIndex import *
from thinfilms import *
from matplotlib.pyplot import *
from numpy import *
import WavePropagation
reload(WavePropagation)
reload(spectrometer)

print 'Building Spectrometer'
thickness = 250e-9
optimizedWavelength = 1550e-9 #spherical aberration correction + blazing
optimizedNeff = max(neff(optimizedWavelength,thickness,nSilica,nSilica,nSi))
optimizedKeff = 2*pi*optimizedNeff/optimizedWavelength
keff = lambda x:2*pi*max(neff(x,thickness,nSilica,nSilica,nSi))/x
#optimizedPoint = array([cos(2*optimizedOutputAngle),sin(2*optimizedOutputAngle)])*radius

wavelengths = linspace(1500e-9,1600e-9,101)
keffs = [keff(w) for w in wavelengths]

s=spectrometer.Spectrometer()
s.radius = 750e-6
s.order = 10
#s.setInputWithThisWavelengthReflectingBack(optimizedKeff,pitch=4e-6)
s.input = spectrometer.Aperture()
s.input.width = 1.2e-6
s.input.angle = 44*pi/180+pi
s.setCenter(s.input)
s.grating=spectrometer.DiffractionGrating()
s.grating.n = 340
s.grating.pitch = 4e-6
s.grating.width = 4e-6
#s.setOutputToAngle(30*pi/180)
#s.grating.aberrationFreeKeff= optimizedKeff
#s.grating.aberrationFreePoint = optimizedPoint
print ' Building Aberration Reduced Grating'
s.makeOneStigmaticPointGrating(optimizedKeff,centralGroove=145)
print ' Calculating Grating Blaze Angles'
s.blazeGratingToPoint(s.grating.aberrationFreePoint)
#fig=figure(1)
wp = WavePropagation.WavePropagator(s)

print 'Calculating output waveguide position, angle and width'
wp.setOutputArray(keffs)
s.plot()
print 'Simulating'
print ' Calculating Eletromagnetic Field on Grating'
wp.propagateToGrating(optimizedKeff)
figure(2)
s.grating.plotField()
swavelengths = linspace(1498e-9,1502e-9,100)
skeffs = [keff(w) for w in swavelengths]

spectrum = wp.transmissionSpectrum(skeffs,s.outputs[0])
figure(3)
plot(swavelengths,spectrum)
##s.output.points = s.grating.aberrationFreePoint
##s.output.widths = [2e-6]
##s.output = s.makeRowlandCircleEdgePoints(65*pi/180,75*pi/180,3e-6,2e-6)
##s.output = s.makeLineAlongWidth(OutputWidth,optimizedOutputAngle,optimizedPoint,100)
##s.plot()
#
#s.output = spectrometer.Aperture()
#s.output.width = 1.4e-6
#s.output.angle = s.grating.aberrationFreeAngle
#s.setCenter(s.output)
#print ' Calculating Eletromagnetic Output'
#
#s.arc = s.setRowlandCircleEdgePoints(75*pi/180,77*pi/180,2e-6,1e-6)
#wp.propagateTo(s.arc)
#figure(1)
##s.arc.plotField()
#
#wp.setAperturePoints(s.output,51)
##wp.propagateTo(s.output)
##figure(2)
##s.output.plotField()
#
##s.image = spectrometer.Grid(10e-6,10e-6,1e-7,s.output.center)
##s.plot()
##s.image.plot()
##wp.propagateTo(s.image)
##figure(3)
##s.image.plotField()
#
#wp.setCosMode(s.output)
#print 'angle2',s.output.angle
#a= wp.findPeakTransmissioAngleAt(keff(1500e-9),s.output)
#wp.propagateTo(s.output)
#s.output.plotField()
#print 'angle',s.output.angle
##print 'angle2',s.output.angle
#
#
##v = s.grating.points[0:-2]-s.grating.points[1:-1]
##print sqrt(sum(v**2,1))
##print sum(abs(s.grating.E[1:-1])**2*sqrt(sum(v**2,1)) )
#print sum(abs(s.input.mode)**2)*(s.input.x[1]-s.input.x[0])
#print sum(abs(s.output.E)**2)*(s.output.x[1]-s.output.x[0])
#
#s.outputs = s.arc
##s.outputs.points = array([s.output.center])
##s.outputs.angles = array([s.output.angle])
##s.outputs.widths = array([s.output.width])
##s.plot()
print 'Saving to File,','spectrometer.txt'
fp  = open('spectrometer.txt','w')
s.write(fp)
fp.close()


